/*
 ============================================================================
 Name        : main.c
 Author      : the anh
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include "type.h"
#include "init.h"
#include <time.h>
#include "metric.h"
//#include "statistic.h"
#include "ns3.h"

//Global variables
int nbOfNodes;		//Number of nodes
struct node* nodes;	//Array with the nodes
int nbOfRadios;		//Number of radios per node
int maxFreq;		//Number of orthogonal frequencies
int algorithm;		//Algorithm number used to allocate the frequencies
float	L;		//Length of a side of the observation window (LxL)
float	R;		//Radio scope of the nodes
int ns3Version=2;	//1: ns-3.5 sans ECMP
			//2: ns-3.7.1 avec ECMP

// The Anh
int disjointPaths; //If 1 there is only one route per node - If 0 we may have several routes using the same node
int directionnels; //If 1 there is conflict directional - If 0 there is conflict bidirectional
// Fin The Anh


void usage(char* string)
{
	fprintf(stderr,"Usage: %s topology nbOfNodes nbOfRadios maxFreq L R nbOfSamples algorithm variant threshold\n",string);
	fprintf(stderr,"-----------------------------------------------------------------\n");
	fprintf(stderr,"-- topology: 0-> random distribution of nodes 1->grid\n");
	fprintf(stderr,"-- nbOfNodes: number of nodes\n");
	fprintf(stderr,"-- nbOfRadios: number of radios\n");
	fprintf(stderr,"-- maxFreq: number of available frequencies\n");
	fprintf(stderr,"-- L: length of the side of the observation window (LxL)\n");
	fprintf(stderr,"-- R: radio scope of the node\n");
	fprintf(stderr,"-- nbOfFiles: number of NS3 file to simulate the capacity\n");
	fprintf(stderr,"-- nbOfRoutes: number of routes between the source and the destination\n");
	fprintf(stderr,"-- algorithm: algorithm number (from 0 to 3) 1Freq=0 Greedy=1 Tabu=2 MCAIR=3 Anh=4\n");
	fprintf(stderr,"-- disjointPaths : If 1 there is only one route per node - If 0 we may have several routes using the same node \n");
	fprintf(stderr,"-- directional : If 1 there is conflict directional - If 0 there is conflict bidirectional \n");
	fprintf(stderr,"-----------------------------------------------------------------\n");
	exit(-1);
}

int main(int argc, char* argv[])
{

	int nbOfSamples,i,topology, nbOfRoutes;
	char fileName[255];

	if((ns3Version!=1)&&(ns3Version!=2)){
		fprintf(stderr,"La version de ns3 est incorrect (=%d et doit etre 1 ou 2)\n",ns3Version);
		exit(15);
	}


	if(argc<12) usage(argv[0]);
	topology=atoi(argv[1]);
	nbOfNodes=atoi(argv[2]);
	nbOfRadios=atoi(argv[3]);
	maxFreq=atoi(argv[4]);
	L=atof(argv[5]);
	R=atof(argv[6]);
	nbOfSamples=atoi(argv[7]);
	nbOfRoutes=atoi(argv[8]);
	algorithm=atoi(argv[9]);
	disjointPaths =atoi(argv[10]);
	directionnels =atoi(argv[11]);

	fprintf(stderr,"--DEBUG-- topology = %d\n",topology);
	fprintf(stderr,"--DEBUG-- nbOfNodes = %d\n",nbOfNodes);
	fprintf(stderr,"--DEBUG-- nbOfRadios = %d\n",nbOfRadios);
	fprintf(stderr,"--DEBUG-- maxFreq = %d\n",maxFreq);
	fprintf(stderr,"--DEBUG-- L = %f R=%f\n",L,R);
	fprintf(stderr,"--DEBUG-- nbOfSamples = %d\n",nbOfSamples);
	fprintf(stderr,"--DEBUG-- nbOfRoutes = %d\n",nbOfRoutes);
	fprintf(stderr,"--DEBUG-- algo = %d\n",algorithm);
	fprintf(stderr,"--DEBUG-- disjointPaths = %d\n",disjointPaths);
	fprintf(stderr,"--DEBUG-- directionnel = %d\n",directionnels);


/*
	topology=1;
	nbOfNodes=16;
	nbOfRadios=2;
	maxFreq=3;
	L=350;
	R=118;
	nbOfSamples=1;
	nbOfRoutes=2;
	algorithm=0;
	disjointPaths = 1;
	directionnels = 0;
*/

	srand(time(NULL));//Must be called only once!
//	srand(8);

	//Malloc for the array nodes
	if((nodes=(struct node *) malloc(nbOfNodes*sizeof(struct node)))==NULL) error("Error: main() -> malloc nodes");

	i=1;
	while(i<=nbOfSamples){
		//Nom du fichier
		if(topology==0)
		  sprintf(fileName,"fichiersNS3/randTopAlgo%dNbradio%dNbFreq%dnbNoeuds%d-%d.cc",algorithm,nbOfRadios,maxFreq,nbOfNodes,i);
		if(topology==1)
		  sprintf(fileName,"fichiersNS3/gridTopAlgo%dNbradio%dNbFreq%dnbNoeuds%d-%d.cc",algorithm,nbOfRadios,maxFreq,nbOfNodes,i);

		fprintf(stderr,"Nom du fichier = %s\n",fileName);
		fprintf(stderr,"Generation du fichier %d sur %d\n",i,nbOfSamples);
		if(NS3file(topology,nbOfRoutes,fileName)==1) i++;


	}//fin du while

	//Dessine dans le fichier graph.eps l'allocation (noeuds et liens) correspondant au dernier fichier
	dessineGraph();

	free(nodes);

	return(0);
}
